package com.version1_1.controller;

import com.util.format.MapToJson;
import com.util.property.Properties;
import com.version1_1.service.SubscriService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by reeves on 2015/8/23.
 */

@Controller
public class SubscriController {

    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(SubscriController.class);

    @Autowired
    private SubscriService subscriService;


    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/getHotSortation/{number}
     *
     * @Description: 获取热门分类
     */
    @RequestMapping(value = "/subscri/getHotSortation/{number}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getHotSortation(@PathVariable("number") int number) throws UnsupportedEncodingException {
        return subscriService.getHotSortation(number);
    }

    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/getSubscriPage/{pageNumber}
     *
     * @Description: 获取推荐界面信息
     */
    @RequestMapping(value = "/subscri/getSubscriPage/{pageNumber}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getSubscriPage(@PathVariable("pageNumber") int pageNumber) throws UnsupportedEncodingException {
        // 客户端请求参数错误
        if (pageNumber <= 0) {
            return "{\"isLast\":true,\"result\":[]}";
        }
        return subscriService.getSubscriPage(pageNumber, Properties.PageSize_MainPage);
    }

    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/getNewSubscriPage
     *
     * @Description: 新接口，获取推荐界面信息
     */
    @RequestMapping(value = "/subscri/getNewSubscriPage", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getNewSubscriPage() throws UnsupportedEncodingException {
        return subscriService.getNewSubscriPage();
    }


    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/subscriThirdType/{userId}/{secondType}/{thirdTypeName}
     *
     * @Description: 订阅单独的三级分类
     */
    @RequestMapping(value = "/subscri/subscriThirdType/{userId}/{secondType}/{thirdTypeName}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String subscriThirdType(@PathVariable("userId") int userId, @PathVariable("secondType") int secondType, @PathVariable("thirdTypeName") String thirdTypeName) {

        return new MapToJson("subscriResult", subscriService.subscriThirdType(userId, secondType, thirdTypeName)).toJsonString();
    }


    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/removeSubscri/{userId}/{secondType}/{thirdTypeName}
     *
     * @Description: 取消订阅单独的三级分类
     */
    @RequestMapping(value = "/subscri/removeSubscri/{userId}/{secondType}/{thirdTypeName}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String removeSubscri(@PathVariable("userId") int userId, @PathVariable("secondType") int secondType, @PathVariable("thirdTypeName") String thirdTypeName) {

        /**
         * {
         *     "removeSubscriResult":true/false
         * }
         */

        return new MapToJson("removeSubscriResult", subscriService.removeSubscri(userId, secondType, thirdTypeName)).toJsonString();
    }


    //TODO: 更改用户订阅排序

    /**
     * 调用接口：http://localhost:8080/kuibuversion1/subscri/changeSubscriOrder/{userId}
     *
     * @Description: 更改用户订阅排序
     */
    @RequestMapping(value = "/subscri/changeSubscriOrder/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String changeSubscriOrder(@PathVariable("userId") int userId, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        /**
         *  updateUserSubscriOrderStr
         *  {
         *    [
         *      {
         *        "firstType":,
         *        "secondTypeOrder":[ //排序没动就发[]
         *          {
         *            "secondType":,
         *            "secondTypeOrder":
         *          }],
         *        "thirdTypeOrder":[ //排序没动就发[]
         *          {
         *            "secondType":,
         *            "thirdNameList":[...]
         *          }...
         *          ]
         *      }...
         *    ]
         *  }
         */
        String updateUserSubscriOrderStr = request.getParameter("updateUserSubscriOrderStr");
        boolean result = false;
        Map<String, Object> changeSubscriOrderMap = new HashMap<>();

        // 服务器未接收到客户端参数
        if (updateUserSubscriOrderStr == null) {
            LOGGER.error("更改用户订阅排序,未接收到参数");
        } else {
            result = subscriService.updateUserSubscriOrder(userId, updateUserSubscriOrderStr);
        }
        return new MapToJson("updateUserSubscriOrderResult", result).toJsonString();
    }

}
